<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>父子组件之间的访问-子访问父</title>
</head>

<body>
    <script src="/js/vue.js"></script>
    <div id="app">
        <p>{{msg}}</p>
        <cpn></cpn>
    </div>

    <template id="cpnT">
        <div>
            <h2>子组件内容</h2>
            <ccpn></ccpn>
        </div>
    </template>

    <template id="ccpnT">
        <div>
            <h2>孙子组件</h2>
            <button @click="btnClick">click</button>
        </div>
    </template>

    <script>
        //孙子组件
        const ccpn = {
            template: `#ccpnT`,
            methods: {
                btnClick(){
                    // 1. 访问父组件$parent
                    console.log(this.$parent);
                    this.$parent.showMessage()

                    //2. $root
                }
            }
        }

        //子组件
        const cpn = {
            template: `#cpnT`,
            components: {
                ccpn
            },
            methods:{
                showMessage(){
                    console.log("message");
                    console.log(this.$parent);
                }
            }
        }


        //父组件
        let app = new Vue({
            el: "#app",
            data: {
                msg: 'hello world'
            },
            components: {
                cpn
            }
        })
    </script>
</body>

</html>